<?php
namespace app\service\controller;

use app\common\controller\ServiceOauth;
use external\SFTools;
use think\App;

class Order extends ServiceOauth {
    public function initialize(){
        parent::initialize();
    }
    
    /**
     * 获取订单列表
     * @param   string  order_number    订单号
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function getOrderList(){
        $order_number = htmlspecialchars(input("post.order_number"));
        $orderLogic = new \backend\logic\OrderLogic;
        $result = $orderLogic->getOrderListBuyOrderNumber($order_number, $this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        } else {
            return SFTools::pullJson(-1, $result['error_code'], $result['data']);
        }
    }
    
    /**
     * 获取订单列表(PC)
     * @param   string  order_number    订单号
     * @param   string  page    页号
     * @param   string  size    页容量
     * @param   boolean  bool    是否需要总条数
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function getOrderListPC(){
        $order_number = htmlspecialchars(input("post.order_number"));
        $page = htmlspecialchars(input("post.page"));
        $size = htmlspecialchars(input("post.size"));
        $bool = htmlspecialchars(input("post.bool"));
        $page = ($page - 1) * $size;
        $orderLogic = new \backend\logic_pc\OrderLogic;
        $result = $orderLogic->getOrderListBuyOrderNumber($order_number, $this->userFind, $page, $size, $bool);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        } else {
            return SFTools::pullJson(-1, $result['error_code'], $result['data']);
        }
    }
    
    /**
     * 获取订单列明细
     * @param   string  order_number    订单号
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function getOrderInfo(){
        $order_number = htmlspecialchars(input("post.order_number"));
        $orderLogic = new \backend\logic\OrderLogic;
        $result = $orderLogic->getOrderFindBuyOrderNumber($order_number, $this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        } else {
            return SFTools::pullJson(-1, $result['error_code'], $result['data']);
        }
    }
    
    /**
     * 获取SPU列表
     * @param   string  spu_name        SPU 名称
     * @param   int     page            页码
     * @param   int     size            每页大小
     * @param   string  type            返回类型 type='count' 返回数据总数
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function getSpuList(){
        $spu_name = htmlspecialchars(input("post.spu_name"));
        $page = htmlspecialchars(input("post.page"));
        $size = htmlspecialchars(input("post.size"));
        $type = htmlspecialchars(input("post.type"));
        $orderLogic = new \backend\logic\OrderLogic;
        $result = $orderLogic->getSpuListLikeSpuName($spu_name, $this->userFind, $page, $size, $type);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        } else {
            return SFTools::pullJson(-1, $result['error_code'], $result['data']);
        }
    }
    
    /**
     * 获取SPU列表
     * @param   string  spu_uuid    SPU 唯一标识
     * @param   string  sku_code    商品二位码
     * @return  json                Json对象 ( status / code / msg / data )
     */
    public function getSkuList(){
        $spu_uuid = htmlspecialchars(input("post.spu_uuid"));
        $sku_code = htmlspecialchars(input("post.sku_code"));
        $orderLogic = new \backend\logic\OrderLogic;
        $result = $orderLogic->getSkuListBuySpuUUIDorSkuCode($spu_uuid, $sku_code, $this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        } else {
            return SFTools::pullJson(-1, $result['error_code'], $result['data']);
        }
    }
    
    /**
     * 获取草稿单信息
     * @param   无
     * @return  json                Json对象 ( status / code / msg / data )
     */
    public function getDraftOrder(){
        $orderLogic = new \backend\logic\OrderLogic;
        $result = $orderLogic->getDraftOrderBuyFccode($this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        } else {
            return SFTools::pullJson(-1, $result['error_code'], $result['data']);
        }
    }

    /**
     * 获取草稿单信息(PC)
     * @param   无
     * @return  json                Json对象 ( status / code / msg / data )
     */
    public function getDraftOrderPC(){
        $orderLogic = new \backend\logic\OrderLogic;
        $result = $orderLogic->getDraftOrderBuyFccodePC($this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        } else {
            return SFTools::pullJson(-1, $result['error_code'], $result['data']);
        }
    }

    /**
     * 获取定制属性
     * @param   string  fStyleCode  款式编码
     * @return  json                Json对象 ( status / code / msg / data )
     */
    public function getCustom(){
        $fStyleCode = htmlspecialchars(input("post.fStyleCode"));
        $orderLogic = new \backend\logic\OrderLogic;
        $result = $orderLogic->getCustomBuyFStyleCode($fStyleCode);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        } else {
            return SFTools::pullJson(-1, $result['error_code'], $result['data']);
        }
    }
    
    /**
     * 添加商品到订单（常规商品）
     * @param   string  order_uuid  订单唯一标识
     * @param   string  goods_id    商品ID
     * @param   string  fOrdQty     商品I数量
     * @return  json                Json对象 ( status / code / msg / data )
     */
    public function addSku(){
        $order_uuid = htmlspecialchars(input("post.order_uuid"));
        $goods_id = htmlspecialchars(input("post.goods_id"));
        $fOrdQty = htmlspecialchars(input("post.fOrdQty"));
        $orderLogic = new \backend\logic\OrderLogic;
        $result = $orderLogic->addSkuToOrder($order_uuid, $goods_id, $fOrdQty, $this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        } else {
            return SFTools::pullJson(-1, $result['error_code'], $result['data']);
        }
    }
    
}
